package com.arqui.admmon_module.client.core.services;

import java.util.List;
import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.arqui.admmon_module.client.place.NameTokens;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.google.inject.Inject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.arqui.admmon_module.client.action.RealTimeServices;
import com.arqui.admmon_module.client.action.RealTimeServicesResult;
import com.arqui.admmon_module.client.core.services.ServicesPresenter;
import com.arqui.admmon_module.shared.ServicesEntity;

public class RealTimeServicesPresenter
		extends
		Presenter<RealTimeServicesPresenter.MyView, RealTimeServicesPresenter.MyProxy> {
	private static final int REFRESH_INTERVAL = 2000;

	public interface MyView extends View {
		public FlexTable getRealTimeT();
		public void setRealTimeT(FlexTable realTimeT);
	}

	@ProxyCodeSplit
	@NameToken(NameTokens.realtimeservices)
	public interface MyProxy extends ProxyPlace<RealTimeServicesPresenter> {
	}

	@Inject
	public RealTimeServicesPresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy) {
		super(eventBus, view, proxy);
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, ServicesPresenter.SLOT_content, this);
	}
	@Inject DispatchAsync dispatchAsync;

	@Override
	protected void onBind() {
		super.onBind();
		FlexTable realtimeT= getView().getRealTimeT(); 
		realtimeT.setText(0, 0, "Servicio");
		realtimeT.setText(0, 1, "Fecha y hora");
		realtimeT.setText(0, 2, "Nombre de la M�quina");
		realtimeT.setText(0, 3, "IP de la M�quina");
		realtimeT.setSize("957px", "27px");
		Timer refreshTimer = new Timer() {
			RealTimeServices action = new RealTimeServices();
			public void run()
			{
				refreshTable();
			}
			private void refreshTable() {
				dispatchAsync.execute(action, getRealTimeServices);
			}
			
		};
		refreshTimer.scheduleRepeating(REFRESH_INTERVAL);
	}
	
	private final AsyncCallback<RealTimeServicesResult> getRealTimeServices = new AsyncCallback<RealTimeServicesResult>() {
		
		@Override
		public void onSuccess(RealTimeServicesResult result) {
			int n= getView().getRealTimeT().getRowCount();
			List<ServicesEntity> events= result.getResult();
			for(int i=n;i<n+events.size();n++){
				getView().getRealTimeT().setHTML(i, 0, events.get(i).getService());
				getView().getRealTimeT().setHTML(i, 1, events.get(i).getDate());
				getView().getRealTimeT().setHTML(i, 2, events.get(i).getNameMachine());
				getView().getRealTimeT().setHTML(i, 3, events.get(i).getIpMachine());
			}
		}
		
		@Override
		public void onFailure(Throwable caught) {
		}
	};
}